package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.LineDao;
import org.elepower.dao.PoleDao;
import org.elepower.pojo.PsLineInfo;
import org.elepower.pojo.PsPoleInfo;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/line")
public class LineController {
    @Autowired
    private LineDao lineDao;
    @Autowired
    private PoleDao poleDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/line/index";
    }

    @RequestMapping("/selectLine")
    @ResponseBody
    public Map<String,Object> getLine(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit, PsLineInfo psLineInfo) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Page<PsLineInfo> pageList = PageHelper.startPage(page,limit);

        List<PsLineInfo> list = lineDao.getlist(psLineInfo);
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);
        map.put("count",pageList.getTotal());
        return map;
    }

    @RequestMapping("/form")
    public String getForm() throws Exception{
        return "system/line/form";
    }

    @RequestMapping(value = "/addLine",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(PsLineInfo psLineInfo, @RequestParam("poleStr") String poleStr,HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //杆塔编号
        String[] pole = poleStr.split(",");

        Integer[] poleIds = new Integer[pole.length];
        for(int i = 0;i < pole.length;i++){
            poleIds[i] = Integer.parseInt(pole[i]);
        }
        //起始杆塔和终止杆塔
        psLineInfo.setStartPoleId(poleIds[0]);
        psLineInfo.setEndPoleId(poleIds[poleIds.length-1]);
        //用户名
        PsUser psUser = (PsUser) session.getAttribute(Constants.LOGIN_USER);
        psLineInfo.setCreatedBy(psUser.getUsername());

        psLineInfo.setMaintenanceCompany("未知");
        //线路状态
        psLineInfo.setRunningStatusId(1);
        psLineInfo.setRunningStatusName("正常");

        if(psLineInfo.getIsStart() == null){
            psLineInfo.setIsStart(0);
        }else{
            psLineInfo.setIsStart(1);
        }

        lineDao.add(psLineInfo);

        System.out.println(psLineInfo.getId());
        for(int i = 0;i < poleIds.length;i++){
            System.out.println(poleIds[i]);
        }

        poleDao.updateLine(psLineInfo.getId(),poleIds);


        map.put("result",true);
        return map;
    };

    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //删除杆塔线路
        poleDao.updateLineNull(id);
        //删除线路
        lineDao.del(id);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/getLine")
    @ResponseBody
    public PsLineInfo getLine(Integer lineId) throws Exception{
        PsLineInfo psLineInfo = lineDao.get(lineId);
        return psLineInfo;
    }

    @RequestMapping("/getLinePole")
    @ResponseBody
    public List<PsPoleInfo> getLinePole(Integer lineId) throws Exception{
        List<PsPoleInfo> poleList = new ArrayList<>();
        poleList = poleDao.getLinePole(lineId);
        return poleList;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsLineInfo psLineInfo,String poleStr) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //杆塔编号
        String[] pole = poleStr.split(",");

        Integer[] poleIds = new Integer[pole.length];
        for(int i = 0;i < pole.length;i++){
            poleIds[i] = Integer.parseInt(pole[i]);
        }
        //起始杆塔和终止杆塔
        psLineInfo.setStartPoleId(poleIds[0]);
        psLineInfo.setEndPoleId(poleIds[poleIds.length-1]);
        //最后更新时间
        psLineInfo.setLastUpdateTime(new Date());

        //更新杆塔
        poleDao.updateLineNull(psLineInfo.getId());
        poleDao.updateLine(psLineInfo.getId(),poleIds);
        //更新线路信息
        lineDao.update(psLineInfo);

        map.put("result",true);
        return map;
    }

    @RequestMapping("/getNormalLine")
    @ResponseBody
    public List<PsLineInfo> getNormalLine() throws Exception{
        return lineDao.getNormalLine();
    }
}
